﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.Routing;
using EPiBoost.Core.Interfaces.ContentInterfaces;
using EPiServer;
using EPiServer.Core; 
using EPiServer.DataAbstraction;
using EPiServer.Globalization;
using EPiServer.ServiceLocation;
using EPiServer.SpecializedProperties;
using EPiServer.Web;
using EPiServer.Web.Routing;

namespace EPiBoost.Core.Extensions
{
    public static class PageDataExtensions
	{
		#region Interfaces



		public static string GetMainTitle(this PageData page)
		{
			if(page is IMainTitle)
			{
				return (page as IMainTitle).MainTitle;
			}
			return page.PageName;
		}

		public static string GetMainShortTitle(this PageData page)
		{
			if (page is IMainShortTitle)
			{
				return (page as IMainShortTitle).MainShortTitle;
			}
			return page.GetMainTitle();
		}


		/// <summary>
		/// checks if a page implements the specified interface
		/// </summary>
		/// <param name="pageData"></param>
		/// <param name="interfaceType"></param>
		/// <returns></returns>
		public static bool ImplementsInterface(this PageData pageData, Type interfaceType)
		{
			var type = pageData.GetOriginalType();
			if (type != null)
			{
				return interfaceType.IsAssignableFrom(type);
			}

			return false;
		}

    	#endregion
		

		#region Get Published / Saved Versions
		public static PageData GetPreviouslyPublishedVersion(this PageReference pageRef)
		{
			PageVersion previousVersion = PageVersion.List(pageRef)
				.FirstOrDefault(pv => pv.Status == VersionStatus.PreviouslyPublished || pv.Status == VersionStatus.Published);

			//return previousVersion != null ? DataFactory.Instance.GetPage(previousVersion.ID, LanguageSelector.AutoDetect(true)) : null;
			var repo = ServiceLocator.Current.GetInstance<IContentRepository>();
			return previousVersion != null ? repo.Get<PageData>(previousVersion.ID, LanguageSelector.AutoDetect(true)) : null;
		}

		public static PageData GetPreviouslySavedVersion(this PageReference pageRef)
		{
			PageVersion previousVersion = PageVersion.List(pageRef).FirstOrDefault();
			
			//return previousVersion != null ? DataFactory.Instance.GetPage(previousVersion.ID, LanguageSelector.AutoDetect(true)) : null;
			var repo = ServiceLocator.Current.GetInstance<IContentRepository>();
			return previousVersion != null ? repo.Get<PageData>(previousVersion.ID, LanguageSelector.AutoDetect(true)) : null;
		}

		public static IEnumerable<PageData> GetPreviousVersions(this PageReference pageRef)
		{
			PageVersionCollection previousVersions = PageVersion.List(pageRef);

			var repo = ServiceLocator.Current.GetInstance<IContentRepository>();
			foreach (PageVersion previousVersion in previousVersions)
			{
				//yield return DataFactory.Instance.GetPage(previousVersion.ID, LanguageSelector.AutoDetect(true));
				yield return repo.Get<PageData>(previousVersion.ID, LanguageSelector.AutoDetect(true));
			}
		}
		#endregion

		#region Url

		/// <summary>
		/// Get the external URL for a page
		/// </summary> 
		public static string GetExternalUrl(this PageData page)
		{
			if(page == null)
			{
				return null;
			}

			Uri uri = new Uri(EPiServer.Configuration.Settings.Instance.SiteUrl, page.LinkURL);
			UrlBuilder url = new UrlBuilder(uri.AbsoluteUri);
			if (url.QueryCollection["id"] != null && int.Parse(url.QueryCollection["id"]) != page.PageLink.ID)
			{
				page.PageLink = new PageReference(int.Parse(url.QueryCollection["id"]));
			}
			EPiServer.Global.UrlRewriteProvider.ConvertToExternal(url, page.PageLink, System.Text.UTF8Encoding.UTF8);
			return url.Uri.AbsoluteUri;
		}

		public static string GetHref(this PageData page)
		{
			string externalUrl = GetExternalUrl(page);
			if (!string.IsNullOrEmpty(externalUrl))
			{
				UrlBuilder url = new UrlBuilder(externalUrl);
				return url.Uri.PathAndQuery;
			}

			return null;
		}

	 
		#endregion
	}


	 

}
